package com.baizhi.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class IdGenerator {
    @Value("${workId}")
    private Integer workerId;
    @Value("${dataCenterId}")
    private Integer datacenterId;
    public Long generateId(){
        //获取到雪花工具类，两个参数一个是机器id，一个是机房id，两个共同构成10位的机器id
        Snowflake sf = IdUtil.getSnowflake(workerId, datacenterId);
        //生成唯一id
        long id = sf.nextId();
        return id;
    }
}
